<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>入库登记</title>
		<link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
		<link rel="stylesheet" href="../../../css/public.css" media="all">
	</head>
	<body>
		<div class="layuimini-container">
			<div class="layuimini-main">
				<!--搜索开始-->
				<fieldset class="layui-elem-field layuimini-search">
					<legend>搜索信息</legend>
					<div style="margin: 10px 10px 10px 10px">
						<form class="layui-form layui-form-pane" action="" id="searchFrm">
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">查询时间</label>
									<div class="layui-input-inline">
										<input type="text" id="registerTime" name="registerTime" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">申请单编号</label>
									<div class="layui-input-inline">
										<input type="text" name="gatherId" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-inline">
									<a class="layui-btn " lay-submit="" lay-filter="data-search-btn"><label class="layui-icon layui-icon-search"></label>搜索</a>
									<a class="layui-btn layui-btn-warm" onclick="javascript:$('#searchFrm')[0].reset()" lay-filter="data-search-btn"><label
										 class="layui-icon layui-icon-refresh"></label>重置</a>
								</div>
							</div>
						</form>
					</div>
				</fieldset>
				<!--搜索结束-->

				<!--数据列表-->
				<table id="PutRepertory" lay-filter="test"></table>

				<!--入库登记-->
				<div class="layui-row" id="update" style="display:none;">
					<div class="layui-col-md10" align="center">
						<form class="layui-form layui-from-pane" style="margin-top: 20px;" id="accountFomat">

							<table width="880px" style="margin-left: 60px;" height="400px">
								<tr>
									<td colspan="2" width="100%">入库单编号：<span id="productId"></span></td>
								</tr>
								<tr>
									<td width="50%">入库理由：<span id="reason"></span></td>
									<td width="50%">入库详细理由：<span id="reasonexact"></span></td>
								</tr>
								<tr>
									<td colspan="2" width="100%">
										<table id="PutRepertoryLog" lay-filter="test"></table>
									</td>
								</tr>
								<tr>
									<td width="50%">应入库总件数：<span id="amountSum"></span></td>
									<td width="50%">已入库总件数：<span id="gatheredAmountSum"></span></td>
								</tr>
								<tr>
									<td width="50%">应入库总成本：<span id="costPriceSum"></span></td>
									<td width="50%">已入库总成本：<span id="subtotal">0</span></td>
								</tr>
								<tr>
									<td width="50%">登记人：<span id="checker"></span></td>
									<td width="50%">登记时间：<span id="checkTime"></span></td>
								</tr>
							</table>
							<div class="layui-form-item" style="margin-top:40px">
								<div class="layui-input-block">
									<input class="layui-btn" type="button" lay-submit="*" lay-filter="dataForm" value="提交">
								</div>
							</div>
						</form>
					</div>
				</div>

			</div>
		</div>

		<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
		<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
		<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
		<script src="../../../lib/common/common.js" charset="utf-8"></script>

		<script type="text/javascript">
			layui.use(['table', 'jquery', 'form', 'laydate'], function() {

				var table = layui.table,
					layer = layui.layer,
					form = layui.form,
					$ = layui.jquery,
					laydate = layui.laydate;

				laydate.render({
					elem: '#registerTime'
				});

				// 监听搜索操作
				form.on('submit(data-search-btn)', function(data) {
					var params = $("#searchFrm").serialize();
					//执行搜索重载
					table.reload('PutRepertory', {
						page: {
							curr: 1
						},
						url: api7 + 'putRepertory/getPutRepertory?' + params
					});
					return false;
				});

				//第一个实例
				var tableIns = table.render({
					elem: '#PutRepertory',
					url: api7 + 'putRepertory/getPutRepertory',
					page: true,
					cols: [
						[{
							field: 'gatherId',
							title: '入库单编号',
							sort: true,
							fixed: 'left',
							align: 'center',
							
						}, {
							field: 'storer',
							title: '入库人',
							align: 'center'
						}, {
							field: 'reason',
							title: '入库理由',
							templet: '#reason1',
							align: 'center'
						}, {
							field: 'checkTime',
							title: '登记时间',
							align: 'center'
						}, {
							field: 'amountSum',
							title: '总件数',
							align: 'center'
						}, {
							field: 'costPriceSum',
							title: '总金额(元)',
							align: 'center'
						}, {
							field: 'id',
							title: '登记',
							templet: '#registerSum',
							align: 'center'
						}]
					]
				});

				//监听行工具事件
				table.on('tool(test)', function(obj) {
					var data = obj.data;

					$.ajax({
						url: api7 + "PutRepertoryDetails/getDetailsListByPid",
						method: "get",
						data: {
							id: data.id
						},
						success: function(res) {

							$("#productId").html(data.gatherId)
							if (data.reason == 'R001-1') {
								$("#reason").html("生产入库")
							} else if (data.reason == 'R001-2') {
								$("#reason").html("库存初始")
							} else if (data.reason == 'R001-3') {
								$("#reason").html("赠送")
							} else if (data.reason == 'R001-4') {
								$("#reason").html("内部归还")
							} else {
								$("#reason").html("其他归还")
							}

							$("#reasonexact").html(data.reasonexact)
							$("#amountSum").html(data.amountSum)

							var amountNow=0;
							for(var i=0; i<res.data.length; i++){
								amountNow+=res.data[i].repertoryCell.amount
							}
							$("#gatheredAmountSum").html(amountNow)

							var subtotalNow=0;
							for(var i=0; i<res.data.length; i++){
								subtotalNow=subtotalNow+res.data[i].repertoryCell.amount*res.data[i].costPrice
							}
							$("#subtotal").html(subtotalNow)
	
							$("#costPriceSum").html(data.costPriceSum)
							$("#checker").html(data.checker)
							$("#checkTime").html(data.checkTime)

							table.render({
								elem: '#PutRepertoryLog',
								url: api7 + "PutRepertoryDetails/getDetailsListByPid?id=" + data.id,
								cols: [
									[{
										field: 'productId',
										title: '产品编号',
										sort: true,
										fixed: 'left',
										align: 'center'
									}, {
										field: 'productName',
										title: '产品名称',
										align: 'center'
									}, {
										field: 'firstKindName',
										title: '存放地址集合',
										templet: '#firstKindName',
										align: 'center'
									}, {
										field: 'storeName',
										title: '库房名称',
										align: 'center',
										templet: '#storeName'
									}, {
										field: 'amount',
										title: '应入库件数',
										align: 'center',
										templet: "#amount"
									}, {
										field: 'gatheredAmount',
										title: '已入件数',
										align: 'center',
										templet:"#amountCell"
									}, {
										field: 'amount',
										title: '本次入库数量',
										align: 'center'
									}]
								]
							});
							
							
									
							form.on('submit(dataForm)', function(data) {
								$.ajax({
									url: api7 + "PutRepertoryDetails/Warehousing",
									method:"post",
									data: {
										id:obj.data.id
									},
									success:function(res) {
										if (res.code == 200) {
											layer.msg(res.msg);
											tableIns.reload();
										}
										layer.close(mainIndex);
									}
								})
							
							})

							if (obj.event === 'edit') {
								mainIndex = layer.open({
									type: 1,
									title: '入库登记',
									area: ['1000px', '650px'],
									content: $("#update")
								});
							}
						}
					})
				});

			})
		</script>

		<script type="text/html" id="reason1">
			{{# if(d.reason=='R001-1'){d.reason='生产入库'}}
				{{d.reason}}
			{{# }else if(d.reason=='R001-2'){d.reason='库存初始'}}
				{{d.reason}}
			{{# }else if(d.reason=='R001-3'){d.reason='赠送'}}
				{{d.reason}}
			{{# }else if(d.reason=='R001-4'){d.reason='内部归还'}}
				{{d.reason}}
			{{# }else{d.reason='其他归还'}}
				{{d.reason}}
			{{# }}}
		</script>

		<script type="text/html" id="storeName">
			{{d.repertoryCell.storeName}}
		</script>

		<script type="text/html" id="amount">
			{{d.amount}}
		</script>

		<script type="text/html" id="firstKindName">
			{{d.repertoryCell.firstKindName+"-"+d.repertoryCell.secondKindName+"-"+d.repertoryCell.thirdKindName}}
		</script>
		
		<script type="text/html" id="amountCell">
			{{d.repertoryCell.amount}}
		</script>

		<script type="text/html" id="gatheredAmount">
			{{# if(d.gatheredAmount==null||d.gatheredAmount==""){d.gatheredAmount=0}}
				{{0}}
			{{# }}}
		</script>

		<script type="text/html" id="registerSum">
			<button class="layui-btn layui-btn-xs" lay-event="edit">登记</button>
		</script>

	</body>
</html>
